package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.model.SysDataSourceDef;
import com.ccp.dev.system.service.SysDataSourceDefService;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * SYS_DATA_SOURCE_DEF 控制器类
 *
 * @author caishaofan
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_SYSTEM + "/dataSourceDef")
public class SysDataSourceDefController extends SysBaseController {

    @Resource
    private SysDataSourceDefService sysDataSourceDefService;

    /**
     * 页面跳转 列表页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/list")
    public String dataSource() {
        return "dataSourceDef/list";
    }

    /**
     * 页面跳转 添加页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/addEdit")
    public String addSource(HttpServletRequest request , Model model)
    {
        String defId = RequestUtil.getString(request,"defId");
        model.addAttribute("defId",defId);
        return "dataSourceDef/addEdit";
    }

    /**
     * 取得SYS_DATA_SOURCE_DEF分页列表
     *
     * @param request 请求
     */
    @ResponseBody
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    public ResultData list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, true);
        List<SysDataSourceDef> list = sysDataSourceDefService.queryList(queryFilter);
        return success(list, queryFilter.getPageBean().getTotalCount());
    }

    /**
     * 根据主键ID删除一条SYS_DATA_SOURCE_DEF数据
     *
     * @param id 参数名称必须是"id"
     * @return java.lang.Object
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    public ResultData del(@RequestParam("id") String id) {
        int i = sysDataSourceDefService.delDataSourceDefById(id);
        if (i > 0) {
            return getResultDataSuccess("delete.success");
        } else {
            return getResultDataError("delete.failure");
        }
    }

    /**
     * 添加或更新SYS_DATA_SOURCE_DEF
     *
     * @param sysDataSourceDef 数据源模板对象
     * @return 返回信息对象
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @ResponseBody
    public ResultData save(@RequestBody SysDataSourceDef sysDataSourceDef,HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, false);
        List<SysDataSourceDef> listDataSourceDef;
        if (StringUtil.isNotEmpty(sysDataSourceDef.getId())) {
            listDataSourceDef = sysDataSourceDefService.queryListRemoveOneId(sysDataSourceDef.getId());
        }else {
            listDataSourceDef = sysDataSourceDefService.queryList(queryFilter);
        }
        String classPath = sysDataSourceDef.getClassPath();
        String name = sysDataSourceDef.getName();
        for (SysDataSourceDef dataSourceDef : listDataSourceDef) {
            if(name.equals(dataSourceDef.getName())&&classPath.equals(dataSourceDef.getClassPath())){
                return getResultDataError("dataSourceDef.nameAndClassPath.repeat");
            }
        }
        int count = sysDataSourceDefService.addOrUpdate(sysDataSourceDef);
        if (count > 0) {
            return getResultDataSuccess("operation.success");
        } else {
            return getResultDataError("operation.failure");
        }
    }

    /**
     * 根据类名获取Setter字段，以json的格式返回,json格式定义如下
     * [{"name":"属性名称","comment":"描述","type":"值类型","baseAttr":"是否基础属性（是否必填）0：false,1:true"},{...},...]
     *
     * @param classPath 类路径
     * @return json数组
     */
    @RequestMapping("/getSetterFields")
    @ResponseBody
    public ResultData getSetterFields(@RequestParam("classPath") String classPath) {
        JSONArray jsonArray = sysDataSourceDefService.getHasSetterFieldsJsonArray(classPath);
        return getResultDataSuccess("query.success", jsonArray);
    }


    /**
     * 验证类
     *
     * @param classPath 类路径
     */
    @RequestMapping("/validClass")
    @ResponseBody
    public ResultData validClass(@RequestParam("classPath") String classPath) {
        boolean rtn = BeanUtils.validClass(classPath);
        if (rtn) {
            return getResultDataSuccess("dataSource.validClass.success");
        } else {
            return getResultDataError("dataSource.validClass.failure");
        }
    }

    /**
     * 获取某个数据源模板信息
     *
     * @param id 数据源模板id
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET + "SysDataSourceDef")
    @ResponseBody
    public ResultData getSysDataSourceDef(@RequestParam("id") String id) {
        SysDataSourceDef sysDataSourceDef = sysDataSourceDefService.getOneById(id);
        if (BeanUtils.isNotEmpty(sysDataSourceDef)) {
            return getResultDataSuccess("query.success", sysDataSourceDef);
        } else {
            return getResultDataError("query.failure");
        }
    }
}
